
#include <iostream>
#include "XJJsonBase.h"

class NetInfo : public XJUtilClass::XJJsonBase
{
public:
    std::string proto;
    std::string ip;
    std::string mask;
    std::string gateway;
    std::string dns;
    int32_t     type;
    NetInfo(){
        BIND_OBJ_FIELD(proto, XJUtilClass::XJJFT_STRING);
        BIND_OBJ_FIELD(ip, XJUtilClass::XJJFT_STRING);
        BIND_OBJ_FIELD(mask, XJUtilClass::XJJFT_STRING);
        BIND_OBJ_FIELD(gateway, XJUtilClass::XJJFT_STRING);
        BIND_OBJ_FIELD(dns, XJUtilClass::XJJFT_STRING);
        BIND_OBJ_FIELD(type, XJUtilClass::XJJFT_INT32);
    } 
    DEF_FUN_NEW(NetInfo);;
};

class DevInfoDU : public XJUtilClass::XJJsonBase
{
public:
    std::string company;
    std::string device;
    std::string name;
    std::string serial;
    std::string system;
    std::string version;
    std::string hardware;
    std::string protocal;
    NetInfo net;
    std::string mac;

    DevInfoDU(){
        BIND_OBJ_FIELD(company, XJUtilClass::XJJFT_STRING);
        BIND_OBJ_FIELD(device, XJUtilClass::XJJFT_STRING);
        BIND_OBJ_FIELD(name, XJUtilClass::XJJFT_STRING);
        BIND_OBJ_FIELD(serial, XJUtilClass::XJJFT_STRING);
        BIND_OBJ_FIELD(system, XJUtilClass::XJJFT_STRING);
        BIND_OBJ_FIELD(version, XJUtilClass::XJJFT_STRING);
        BIND_OBJ_FIELD(hardware, XJUtilClass::XJJFT_STRING);
        BIND_OBJ_FIELD(protocal, XJUtilClass::XJJFT_STRING);
        BIND_OBJ_FIELD(net, XJUtilClass::XJJFT_OBJ);
        BIND_OBJ_FIELD(mac, XJUtilClass::XJJFT_STRING);
    } 
    DEF_FUN_NEW(DevInfoDU);
};

class NetInfoListDU : public XJUtilClass::XJJsonBase {
   public:
    std::list<NetInfo*> nets;
    NetInfoListDU() {
        BIND_ARR_FIELD(nets, XJUtilClass::XJJFT_LIST_OBJ, new NetInfo())
    }
    virtual ~ NetInfoListDU() {
        for (std::list<NetInfo*>::iterator it = nets.begin(); it!= nets.end(); it++) {
            delete *it;
        }
        nets.clear();
    }
    DEF_FUN_NEW(NetInfoListDU);
};

int main(int argc, char *argv[])
{
    DevInfoDU du;
    du.company = "XiujieCompany";
    du.device = "DeviceType";
    du.name = "DeviceName";
    du.serial = "100001";
    du.system = "1.0";
    du.version = "1.0";
    du.hardware = "1.0";
    du.protocal = "1.0";
    du.mac = "cd:22:33:44:55:99:99:ab";
    du.net.ip = "192.168.0.11";
    du.net.proto = "static";
    du.net.gateway = "192.168.0.1";
    du.net.dns = "114.114.114.114";
    du.net.mask = "255.255.255.0";
    du.net.type = 1;
    DevInfoDU du2;
    du2.UnPack(du.Pack());

    NetInfo *net = new NetInfo();
    net->UnPack(du.net.Pack());
    NetInfo *net2 = new NetInfo();
    net2->UnPack(du.net.Pack());
    NetInfoListDU du3;
    du3.nets.push_back(net);
    du3.nets.push_back(net2);
    NetInfoListDU du4;
    du4.UnPack(du3.Pack());
    std::cout << __func__ << " du=" << du.Pack() << " du2=" << du2.Pack() << " du2.net.ip =" << du2.net.ip << std::endl;
    std::cout << __func__ << " du3=" << du3.Pack() << std::endl;
    std::cout << __func__ << " du4=" << du4.Pack() << std::endl;

    du2.UnPack("{\"company\":\"XiujieCompany222\"}");
    std::cout << __func__ <<" du2=" << du2.Pack() << " du2.net.ip =" << du2.net.ip << std::endl;

}
